//
//  JMHttpTool.m
//  萌妹微博
//
//  Created by 张家铭 on 15/12/8.
//  Copyright © 2015年 张家铭. All rights reserved.
//

#import "JMHttpTool.h"
#import "AFNetworking/AFNetworking.h"
#import "JMAes128.h"
#import "MBProgressHUD+JM.h"

@implementation JMHttpTool


+(NSString *)staues{
     AFNetworkReachabilityManager *manager = [AFNetworkReachabilityManager sharedManager];
    
    [manager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        switch (status) {
            case AFNetworkReachabilityStatusNotReachable: {
                //                [weakSelf loadMessage:@"网络不可用"];
                NSLog(@"网络不可用");
                break;
            }
                
            case AFNetworkReachabilityStatusReachableViaWiFi: {
                //                [weakSelf loadMessage:@"Wifi已开启"];
                NSLog(@"Wifi已开启");
                break;
            }
                
            case AFNetworkReachabilityStatusReachableViaWWAN: {
                //                [weakSelf loadMessage:@"你现在使用的流量"];
                NSLog(@"你现在使用的流量");
                break;
            }
                
            case AFNetworkReachabilityStatusUnknown: {
                //                [weakSelf loadMessage:@"你现在使用的未知网络"];
                NSLog(@"你现在使用的未知网络");
                break;
            }
                
            default:
                break;
        }
    }];
    [manager startMonitoring];
    return nil;
}
+(void)get:(NSString *)url param:(NSDictionary *)params success:(void(^)(id responseObject))success frail:(void(^)(NSError* error))failure {
    
    
    // 检测网络是否可用
    
     AFNetworkReachabilityManager *managerNet = [AFNetworkReachabilityManager sharedManager];
     [managerNet setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
         if (status == AFNetworkReachabilityStatusNotReachable) {
             [MBProgressHUD showError:@"网络不可用"];
             return ;
         }else if (status == AFNetworkReachabilityStatusUnknown){
             [MBProgressHUD showError:@"现在使用的未知网络"];
             return;
         }
         
     }];
    
    
    // 获得请求管理者
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    
    // 发送get请求
    [manager GET:url parameters:params success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
        if (success) {
            success(responseObject);
        }
    } failure:^(AFHTTPRequestOperation * _Nullable operation, NSError * _Nonnull error) {
        if (failure) {
            failure(error);
        }
    }];
}

+(void)post:(NSString *)url param:(NSDictionary *)params success:(void (^)(id responseObject))success frail:(void (^)(NSError* error))failure {
    
    
//    // 检测网络是否可用
//    
//    AFNetworkReachabilityManager *managerNet = [AFNetworkReachabilityManager sharedManager];
//    [managerNet setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
//        if (status == AFNetworkReachabilityStatusNotReachable) {
//            [MBProgressHUD showError:@"网络不可用"];
//            return ;
//        }else if (status == AFNetworkReachabilityStatusUnknown){
//            [MBProgressHUD showError:@"现在使用的未知网络"];
//            return;
//        }
//
//    }];

    // 获得请求管理者
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    
    // 发送post请求
    [manager POST:url parameters:params success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
        if (success) {
            success(responseObject);
        }
    } failure:^(AFHTTPRequestOperation * _Nullable operation, NSError * _Nonnull error) {
        if (failure) {
            failure(error);
        }
    }];
}

+ (void)requestWithParameter:(NSMutableArray *)param success:(void (^)(JMParam *))success failure:(void (^)(NSError *))failure {
    // 拼接Sign
    NSString *str = [NSString stringWithStringArray:param];
    
    // str经过MD5加密获取Sign
    NSString *Sign = [str md5String];
    
    // 用Sign替换掉Token并得到拼接的字符串
    [param replaceObjectsInRange:NSMakeRange(param.count - 2, 2) withObjectsFromArray:@[@"Sign", Sign]];
    NSString *str1 = [NSString stringWithStringArray:param];
    
    // str1经过Aes128加密得到msg
    NSString *msg = [JMAes128 Aes128Encrypt:str1];;
    
    // 去掉msg因为加密产生的多余空格和“<”以及“>”
    NSMutableString *msgs = [msg deleateOccurrencesOfStringArray:@[@" ",@"<",@">"]];
    
    // 设置请求参数
    NSDictionary *dict = @{@"msg":msgs};
    
    // 发送请求
    [JMHttpTool post:httpURL param:dict success:^(id responseObject) {
        // 解析服务器返回的数据
        NSString *s = [NSString stringWithFormat:@"%@", responseObject[@"Msg"]];
        NSData *data =[s hexToBytes];
        NSData *respons = [JMAes128 Aes128Decrypt:data];
        NSError *error;
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:respons options:NSJSONReadingMutableContainers error:&error];
        NSDictionary *s1 =  dict[@"Params"];
        NSString *s2 = [NSString stringWithFormat:@"%@", dict[@"Error"]];
        if ([s2 isEqualToString:@"0"]) { // Token正常
            JMParam *param = [NSClassFromString(@"JMParam") mj_objectWithKeyValues:s1];
            if (success) {
                success(param);
            }
        } else { // Token异常
            JMParam *params = [[JMParam alloc] init];
            params.Error = s2;
            if (success) {
                success(params);
            }
        }
        
    } frail:^(NSError *error) {
        if (failure) {
            failure(error);
        }
    }];

    // 检测网络状态
//    AFNetworkReachabilityManager *reachManager = [AFNetworkReachabilityManager sharedManager];
//    [reachManager startMonitoring];
//    [reachManager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
//        switch (status) {
//            case AFNetworkReachabilityStatusReachableViaWiFi:
//                //JMLog(@"WIFI");
//                break;
//                
//            case AFNetworkReachabilityStatusReachableViaWWAN:
//                //JMLog(@"自带网络");
//                break;
//                
//            case AFNetworkReachabilityStatusNotReachable:
//                [MBProgressHUD showError:@"请检查您的网络"];
//                return;
//                break;
//                
//            case AFNetworkReachabilityStatusUnknown:
//                //JMLog(@"未知网络");
//                break;
//            default:
//                break;
//        }
//    }];
}

@end
